package org.lcp.gatlingqueue.impl.part;

import org.lcp.gatlingqueue.interfaces.part.Barrel;
import org.lcp.gatlingqueue.interfaces.part.Turnplate;

/**
 * 转盘
 * 
 * @author lcp
 * 
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TurnplateImpl implements Turnplate {

	private Barrel currentBarrel;
	private int barrelCount = 0;

	@Override
	public Barrel next() {
		Barrel currentClip = currentBarrel;
		currentBarrel = currentBarrel.getNext();
		return currentClip;
	}

	public int getCount() {
		return barrelCount;
	}

	@Override
	public void addBarrel(Barrel barrel) {
		if (currentBarrel == null) {
			currentBarrel = barrel;
			currentBarrel.setNext(currentBarrel);
			barrelCount++;
			return;
		}
		Barrel tmpCurrent = currentBarrel.getNext();
		Barrel tmpBarrel = barrel;

		while (tmpBarrel.getNext() != null) {
			tmpBarrel = tmpBarrel.getNext();
			barrelCount++;
		}
		currentBarrel.setNext(barrel);
		tmpBarrel.setNext(tmpCurrent);
	}
}
